const askedHttp = require("../../../utils/askedHttp")
Page({
  data: {
    index: 0, // 题目序列
    chooseValue: [], // 选择的答案序列
    totalScore: 100, // 总分
    wrongList: [], // 错误的题目集合
    colorstyle: "", //回答正确或错误的样式
  },
  onLoad: async function (options) {
    var index = Math.floor(Math.random() * 45)
    console.log(index);
    var ask = await askedHttp.getQuestionHttp();
    console.log(ask.data.res);
    this.setData({
      questionList: ask.data.res.slice(index, index + 5) // 拿到答题数据
    })
  },
  /*
   * 单选事件
   */
  radioChange(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    this.data.chooseValue[this.data.index] = e.detail.value;
  },
  //  下一题/提交 按钮

  nextSubmit() {
    //清空上一题答案
    this.setData({
      show: "",
      isCorrect: ""
    })
    // 如果没有选择
    if (this.data.chooseValue[this.data.index] == undefined || this.data.chooseValue[this.data.index].length == 0) {
      wx.showToast({
        title: '请选择至少一个答案!',
        icon: 'none',
        duration: 2000,
        success: function () {
          return;
        }
      })
      return;
    }

    // 判断答案是否正确
    this.chooseError();

    //判断是不是最后一题
    if (this.data.index < this.data.questionList.length - 1) {
      //渲染下一题
      this.setData({
        index: this.data.index + 1
      })
    } else {
      // 跳转到结果页
      console.log(this.data.wrongList);
      let wrongList = JSON.stringify(this.data.wrongList);
      console.log(wrongList);
      let chooseValue = JSON.stringify(this.data.chooseValue);
      let total = this.data.totalScore
      wx.navigateTo({
        url: `../result/result?totalScore=${total}&wrongList=${wrongList}&chooseValue=${chooseValue}`
      })
      // 设置缓存
      var logs = wx.getStorageSync('logs') || []
      console.log(logs);
      let logsList = {
        "date": Date.now(),
        "score": this.data.totalScore
      }
      logs.unshift(logsList);
      wx.setStorageSync('logs', logs);
    }
  },
  /*
   * 错题处理
   */
  chooseError() {
    var trueValue = this.data.questionList[this.data.index]['true'];
    var chooseVal = this.data.chooseValue[this.data.index];
    if (chooseVal.toString() != trueValue.toString()) {
      this.data.wrongList.push(this.data.index);
      this.setData({
        totalScore: this.data.totalScore - this.data.questionList[this.data.index]['scores'] // 减分操作
      })
    }
  },
  // 查看答案是否正确
  confirm() {
    var trueValue = this.data.questionList[this.data.index]['true'];
    var chooseVal = this.data.chooseValue[this.data.index];
    var show = '选择了' + chooseVal + '答案是' + trueValue;
    var isCorrect, colorstyle
    if (chooseVal == trueValue) {
      isCorrect = "回答正确",
        colorstyle = "green"
    } else {
      isCorrect = "回答错误",
        colorstyle = "red"
    }
    this.setData({
      show,
      isCorrect,
      colorstyle
    })
    console.log(show);
  },
  /*
   * 退出答题 按钮
   */
  outTest: function () {
    wx.showModal({
      title: '提示',
      content: '确定退出答题吗,已答对的题目将做无效处理？',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.switchTab({
            url: '../../indexs/index/index'
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

})